class SceneLevel extends eui.Component {
    //单例
    private static shared: SceneLevel;
    public static Shared() {
        if(SceneLevel.shared == null) {
            SceneLevel.shared = new SceneLevel();
        }
        return SceneLevel.shared;
    }

    public sc_level:eui.Scroller;
    public gp_level:eui.Group;
    public img_arrow:eui.Image;
    public btn_back:eui.Button;
    private sel_level: number = 0;
    private LevelIcons:LevelIcon[] = [];

    public constructor() {
        super();
        this.skinName = "src/Game/SceneLevels.exml";
        this.btn_back.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onclick_back,this);
        //Scroller关闭水平方向滚动
        this.sc_level.scrollPolicyH=eui.ScrollPolicy.OFF;
        //创建地图选项
        var row = 20;
        var col = 10;
        var icon_width = this.width / col;      //计算行x间隔
        var icon_height = this.height / row;     //计算列y间隔
        //创建group添加到scroller上
        var group = new eui.Group();//地图背景
        group.width = this.width;
        group.height = icon_height * LevelDataManager.getInstance().totalLevels;//算出最大尺寸
        //填充背景
        for(var i = 0;i <= (group.height / this.height) ;i++) {
            var img = new eui.Image();
            img.source = RES.getRes("GameBG2_jpg");
            img.y = i * this.height;
            img.touchEnabled = false;
            this.gp_level.addChildAt(img,0);
        }
       //获取当前游戏进度
        var milestone: number = LevelDataManager.getInstance().Milestone;
         //以正弦曲线绘制关卡图标的路径
        for(var i = 0; i<LevelDataManager.getInstance().totalLevels;i++){ 
            var icon = new LevelIcon();
            icon.Level = i + 1;
            icon.x = Math.sin(icon_height*i/2/ 180 * Math.PI) * 200 + group.width / 2;
            icon.y = group.height - icon_height*i - icon_height;
            group.addChild(icon);
            icon.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onTabIcon,this);
            //依据进度设置关卡显示
            icon.enabled = i < milestone;
            //保存到一个列表中
            this.LevelIcons.push(icon);
        }
        //开启位图缓存模式
        group.cacheAsBitmap = true;
        this.gp_level.addChild(group);
        //卷动到最底层
        this.gp_level.scrollV = group.height - this.height;
        //让目前的进度处于屏幕最上端
        if(milestone>20){
            this.gp_level.scrollV=group.height-milestone*icon_height;
        }
        //跟踪箭头
        this.img_arrow = new eui.Image();
        this.img_arrow.source = RES.getRes("PageDownBtn_png");

        this.img_arrow.anchorOffsetX = this.img_arrow.width/2;
        this.img_arrow.anchorOffsetY = this.img_arrow.height;

        this.img_arrow.touchEnabled = false;
        let curIcon:LevelIcon=this.LevelIcons[milestone-1];
        this.img_arrow.x = curIcon.x+curIcon.width/2;
        this.img_arrow.y = curIcon.y;
        //将箭头显示在最上层
        this.gp_level.addChild(this.img_arrow);
        //设置当前选择的关卡
        this.sel_level=milestone;
    }
    private onclick_back() {
        SoundManager.getInstance().playClick();
        this.parent.addChild(SceneBegin.getInstance());
        this.parent.removeChild(this);
    }
    //点击关卡按钮
    private onTabIcon(e:egret.TouchEvent){
        //播放点击音效
        SoundManager.getInstance().playClick();
        //获取当前点击的关卡
        let tg_icon:LevelIcon = <LevelIcon>e.currentTarget;
        this.sel_level = tg_icon.Level;
        //设置箭头的位置为当前点击关卡的位置
        this.img_arrow.x = tg_icon.x+tg_icon.width/2;
        this.img_arrow.y = tg_icon.y;
        //跳转场景
        this.parent.addChild(SceneGame.getInstance());
        this.parent.removeChild(this);
        SceneGame.getInstance().initLevelData(tg_icon.Level);
    }
    //打开指定的关卡，如果大于最远关卡，则保存数据也跟着调整
    public OpenLevel(level:number){
        var icon = this.LevelIcons[level - 1];
        icon.enabled = true;
        //同时将选定标记置于其上
        this.img_arrow.x = icon.x;
        this.img_arrow.y = icon.y;
        this.sel_level = icon.Level;
        if(level > LevelDataManager.getInstance().Milestone){
            LevelDataManager.getInstance().Milestone = level;
        }
    }
    public setMileStoneLevel(level:number){
        var icon=this.LevelIcons[level-1];
        icon.enabled=true;
        this.img_arrow.x=icon.x+icon.width/2;
        this.img_arrow.y=icon.y;
        if(level>LevelDataManager.getInstance().Milestone){
            LevelDataManager.getInstance().Milestone=level; 
        }
    }
}